<?php
$page_type = "";
require_once 'header.php';

$cid = "";
$cid = $_SESSION['user_id'];

//Find the Client's SID
$sql = "select c.sid from CLIENT c where c.client_id = '$cid'";
$result_array = execute_sql_in_oracle($sql);
$result = $result_array["flag"];
$cursor = $result_array["cursor"];
if(!$result){
  display_oracle_error_message($cursor);
  die("SID query failed.");
}
$values = oci_fetch_array($cursor);
$sid = $values[0];
oci_free_statement($cursor);

// Enrollment Input Validation
$input_enroll = "";

//Find seqid, available seats, and prerequisites for each checked box
if (!empty($_POST['input_enroll']))
  foreach ($_POST['input_enroll'] as $input_enroll){

//Check current courses
    $already_enrolled = 0;
    $sql = "select count(*) from TAKEN t where t.sid = '$sid' and t.seqid = '$input_enroll'";
    $result_array = execute_sql_in_oracle($sql);
    $result = $result_array["flag"];
    $cursor = $result_array["cursor"];
    if(!$result){
      display_oracle_error_message($cursor);
      die("Current courses query failed");
    }
    $values = oci_fetch_array($cursor);
    $already_enrolled = $values[0];
    oci_free_statement($cursor);
    if($already_enrolled > 0){
      echo("<br>Course Seqid: $input_enroll : Enrollment Failure - Already enrolled in this course<br>");
      continue;
    }

//Check deadline
    $sql = "select deadline from COURSE_OFFERING co where co.seqid = '$input_enroll'";
    $result_array = execute_sql_in_oracle($sql);
    $result = $result_array["flag"];
    $cursor = $result_array["cursor"];
    if(!$result){
      display_oracle_error_message($cursor);
      die("Deadline query failed");
    }
    $values = oci_fetch_array($cursor);
    $deadline = $values[0];
    oci_free_statement($cursor);
    if(strtotime($deadline) < strtotime('now')){
      echo("<br>Course Seqid: $input_enroll : Enrollment Failure - The deadline has passed for this course<br>");
      continue;
    }
	
//Find $prereq
    $prereq = "";
    $need_prereq = 0;
    $needed_prereq = "";
    $sql = "select p.cno2 from COURSE_OFFERING co, COURSE_DESCRIPTION cd, PREREQUISITE p where co.seqid = '$input_enroll' and co.cno = cd.cno and cd.cno = p.cno";
    $result_array = execute_sql_in_oracle($sql);
    $result = $result_array["flag"];
    $cursor = $result_array["cursor"];
    if(!$result){
      display_oracle_error_message($cursor);
      die("Available seat query failed");
    }
    while($values = oci_fetch_array($cursor)) {
      $prereq = $values[0];

//Ensure client has completed each prereq
      $sql2 = "select count(*) from TAKEN t, COURSE_OFFERING co where t.sid = '$sid' and t.seqid = co.seqid and co.cno = '$prereq'";
      $result_array2 = execute_sql_in_oracle($sql2);
      $result2 = $result_array2["flag"];
      $cursor2 = $result_array2["cursor"];
      if(!$result2){
        display_oracle_error_message($cursor2);
        die("Prerequisite Count query failed");
      }
      $values2 = oci_fetch_array($cursor2);
      $prereq_count = $values2[0];
      if($prereq_count == 0) {
        $need_prereq = 1;
        $needed_prereq = $prereq;
      }
      oci_free_statement($cursor2);
    }
    oci_free_statement($cursor);
	
//Apply LDW to TAKEN
    $sql = "select * from TAKEN where seqid = '$input_enroll' for update";

//Find $avail_seats
    $sql = "select max_seats - (select count(*) from taken t where t.seqid = '$input_enroll') from course_offering co where co.seqid = '$input_enroll'";
    $result_array = execute_sql_in_oracle($sql);
    $result = $result_array["flag"];
    $cursor = $result_array["cursor"];
    if(!$result){
      display_oracle_error_message($cursor);
      die("Available seat query failed");
    }
    $values = oci_fetch_array($cursor);
    $avail_seats = $values[0];
    oci_free_statement($cursor);

//Check prerequisites and seat availability
    if($avail_seats > 0 && $need_prereq == 0) {
      $sql = "insert into TAKEN (sid, seqid) values('$sid', '$input_enroll')";
      $result_array = execute_sql_in_oracle($sql);
      $result = $result_array["flag"];
      $cursor = $result_array["cursor"];
      if(!$result){
        $sql = "rollback";
        echo("<br>Course Seqid: $input_enroll : Enrollment Failure<br>");
        if($avail_seats <= 0)
          echo("<br>Class is full<br>");
        else if ($need_prereq != 0)
          echo("<br>Prerequisite required: '$needed_prereq'<br>");
      }
      else {
        $sql = "commit";
        echo("<br>Course Seqid: $input_enroll : Enrollment Succesful<br>");
      }
    }
    else {
      $sql = "rollback";
      echo("<br>Course Seqid: $input_enroll : Enrollment Failure<br>");
      if($avail_seats <= 0)
        echo("<br>Class is full<br>");
      else if ($need_prereq != 0)
        echo("<br>Prerequisite required: '$needed_prereq'<br>");
    }
    $result_array = execute_sql_in_oracle($sql);
    $result = $result_array["flag"];
    $cursor = $result_array["cursor"];
    if(!$result){
      display_oracle_error_message($cursor);
      die("Insert failed");
    }
  }
echo("
  <form method=\"post\" action=\"enroll.php\">
  <input type=\"submit\" value=\"Back\">
  </form>
");
?>